package framework.serialize.protobuf;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ProtobufTest {

    static class Cat {

        public String name;

        public int age;

        public List<Cat> children = new ArrayList<>();
    }

    public static void main(String[] args) {

        Cat cat1 = new Cat();
        cat1.name = "cat1";
        cat1.age = 19;

        Cat cat2 = new Cat();
        cat2.name = "cat2";
        cat2.age = 20;

        Cat cat3 = new Cat();
        cat3.name = "cat3";
        cat3.age = 21;
        cat3.children = Arrays.asList(cat1, cat2);

        ProtobufSerializer protobufSerializer = new ProtobufSerializer();
        byte[] bytes = protobufSerializer.serialize(cat3);
        System.out.println(hexDump(bytes));
        Cat ca4 = protobufSerializer.deserialize(bytes, Cat.class);
        System.out.println(ca4.toString());
    }

    public static String hexDump(byte[] bytes) {
        String hexString = "";
        for (byte b : bytes) {
            hexString += String.format("%02x", b);
        }
        return hexString;
    }

}
